package Olivia.olivia3d.model
{
	public class ORotationMat
	{
		public var m11:Number;public var m12:Number;public var m13:Number;
		public var m21:Number;public var m22:Number;public var m23:Number;
		public var m31:Number;public var m32:Number;public var m33:Number;
		
		/**
		 *  
		 * @param arr= [3x3]
		 * 
		 */		
		public function ORotationMat(arr:Array=null)
		{
			Mat = arr;
		}
		
		public function get Mat():Array
		{
			return [m11,m12,m13,m21,m22,m23,m31,m32,m33];
		}
		
		public function set Mat(arr:Array):void
		{
			if(arr && arr.length>=9){
				m11=arr[0];m12=arr[1];m13=arr[2];
				m21=arr[3];m22=arr[4];m23=arr[5];
				m31=arr[6];m32=arr[7];m33=arr[8];
			}else{
				identity();
			}
		}
		
		private function identity():void
		{
			// TODO Auto Generated method stub
			m11 = m22 = m33 = 1;
			m12 = m13 =  m21 = m23 = m31 = m32 =0;
		}
		
		public function setup_left(rot:Array):void{
			//左手坐标系
			var sh:Number=Math.sin(rot[0]);
			var ch:Number=Math.cos(rot[0]);
			var sp:Number=Math.sin(rot[1]);
			var cp:Number=Math.cos(rot[1]);
			var sb:Number=Math.sin(rot[2]);
			var cb:Number=Math.cos(rot[2]);
			//
			m11=ch*cb+sh*sp*sb;
			m12=-ch*sb+sh*sp*cb;			
			m13=sh*cp;	
			//
			m21=sb*cp;
			m22=cb*cp;
			m23=-sp;
			//
			m31=-sh*cb+ch*sp*sb;
			m32=sb*sh+ch*sp*cb;
			m33=ch*cp;
		}
		
		public function setup_right(rots:Array):void{
			var angle:Number = rots[2];
			var sy:Number = Math.sin(angle);
			var cy:Number = Math.cos(angle);
			angle = rots[1];
			var sp:Number = Math.sin(angle);
			var cp:Number = Math.cos(angle);
			angle = rots[0];
			var sr:Number = Math.sin(angle);
			var cr:Number = Math.cos(angle);
			// matrix = (Z * Y) * X
			//trace(rots);
			//trace(sy,cy,sp,cp);
			//
			m11 = cp * cy;
			m21 = cp * sy;
			m31 = -sp;
			m12 = sr * sp * cy + cr * -sy;
			m22 = sr * sp * sy + cr * cy;
			m32 = sr * cp;
			m13 = (cr * sp * cy + -sr * -sy);
			m23 = (cr * sp * sy + -sr * cy);
			m33 = cr * cp;
		}
	}
}